#ifndef SRF08_H
#define SRF08_H

/*********************************************************************************
   #     # #     #    ######  ######   #####     #     #  #####  #######  #####
   ##   ## #  #  #    #     # #     # #     #    ##   ## #     # #       #     #
   # # # # #  #  #    #     # #     # #          # # # #       # #       #
   #  #  # #  #  #    ######  ######  ######     #  #  #  #####  ######  ######
   #     # #  #  #    #   #   #       #     #    #     # #             # #     #
   #     # #  #  #    #    #  #       #     #    #     # #       #     # #     #
   #     #  ## ##     #     # #        #####     #     # #######  #####   #####
**********************************************************************************
01001101 01010111  01010010 01010000 00110110  01001101 00110010 00110101 00110110 
**********************************************************************************
**********************************************************************************
##################      RP6 ROBOT SYSTEM - RP6 CONTROL M256      #################
##################         RoboBridge Project Version 1.0        #################
**********************************************************************************
##################                    SRF08.H V1.0               #################
**********************************************************************************
*/
#include <stdbool.h>
#include <stdint.h>

#define I2C_DATAID_SRF08		1

#define MAKE_SRF08(SRF, ADDRESS, STOPWATCH)  SRF=createSRF08(ADDRESS,STOPWATCH)

typedef struct srf_str srf08_t;
uint8_t messageBuf[8];

struct srf_str{
	uint8_t address;
	uint8_t initialized;
	uint8_t(*checkData)(uint8_t dataRequestId, srf08_t *srf);
	void (*measure)(srf08_t *srf);
	uint8_t isMeasuring;
	uint16_t range;
	uint8_t light;
	volatile uint16_t *stopwatch;
};


uint8_t srf08_checkI2CDataReady(uint8_t dataRequestId,srf08_t *srf){
	if(dataRequestId==I2C_DATAID_SRF08){
		I2CTWI_getReceivedData(messageBuf, 3);
		srf->light=messageBuf[0];
		srf->range= messageBuf[2]+ (messageBuf[1] << 8);
		srf->isMeasuring=false;
		return true;			
	}
	return false;
}

void srf08_measure(srf08_t *srf){
	if(!srf->isMeasuring)
	{
		if(TWI_operation == I2CTWI_NO_OPERATION)
		{
			I2CTWI_transmit2Bytes(srf->address, 0, 0x51);
			srf->isMeasuring = true;
			*(srf->stopwatch)=0;
		}
	}
	else if(*(srf->stopwatch) >= 70)
	{
		I2CTWI_requestRegisterFromDevice(srf->address, I2C_DATAID_SRF08, 1, 3);
		*(srf->stopwatch)=0;
	}
}

void initializeSRF08(srf08_t *srf, uint8_t addressP,volatile uint16_t *stopwatchP){
	srf->initialized=true;
	srf->address=addressP;
	srf->isMeasuring=false;
	srf->range=0;
	srf->light=0;
	srf->checkData=&srf08_checkI2CDataReady;
	srf->measure=&srf08_measure;
	srf->stopwatch=stopwatchP;
}


srf08_t *createSRF08(uint8_t addressP,volatile uint16_t *stopwatchP){
	srf08_t *ret=(srf08_t*)malloc(sizeof(srf08_t));
	initializeSRF08(ret, addressP, stopwatchP);
	return ret;
}


#endif